package com.uwang.library.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.uwang.library.dao.IndexCountDao;
import com.uwang.library.util.DruidUtil;

public class IndexCountDaoImpl implements IndexCountDao {
	PreparedStatement preparedStatement;
	ResultSet resultSet = null;

	@Override
	public int getCount(Date deta) {
		int i = 0;
		try {
			Connection connection= DruidUtil.getConnection();
			String sql = "SELECT count(1) num FROM borrowing WHERE DATE_FORMAT(borrowing_time,'%Y-%m-%d') = DATE_FORMAT(?,'%Y-%m-%d')";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setTimestamp(1, new Timestamp(deta.getTime()));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				i = resultSet.getInt("num");
				} 
			}catch(SQLException e) {
				e.printStackTrace();
			}
		
		return i;
	}

	@Override
	public int getMemberCount(Date date) {
		int i = 0;
		try {
			Connection connection= DruidUtil.getConnection();
			String sql = "SELECT count(1) num FROM member WHERE DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(?,'%Y-%m-%d')";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				i = resultSet.getInt("num");
				} 
			}catch(SQLException e) {
				e.printStackTrace();
			}
		
		return i;
	}

	@Override
	public int getBookCount(Date date) {
		int i = 0;
		try {
			Connection connection= DruidUtil.getConnection();
			String sql = "SELECT count(1) num FROM books WHERE DATE_FORMAT(putaway_time,'%Y-%m-%d') = DATE_FORMAT(?,'%Y-%m-%d') ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				i = resultSet.getInt("num");
				} 
			}catch(SQLException e) {
				e.printStackTrace();
			}
		
		return i;
	}

	@Override
	public int getTimeoutCount(Date date) {
		int i = 0;
		try {
			Connection connection= DruidUtil.getConnection();
			String sql = "SELECT count(1) num FROM borrowing WHERE (DATE_FORMAT(borrowing_time,'%Y-%m-%d') = DATE_FORMAT(?,'%Y-%m-%d')) and NOW() > term and status >= 1";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				i = resultSet.getInt("num");
				} 
			}catch(SQLException e) {
				e.printStackTrace();
			}
		
		return i;
	}
	
}
